/**
 * 
 */
package com.smzd.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author YuZhongda
 * 
 */
public class SessionHolder {
	private static final ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();
	private static final Map<String, Object> defaultValues = new HashMap<String, Object>();

	/**
	 * @return the defaultvalues
	 */
	static Map<String, Object> getDefaultvalues() {
		return defaultValues;
	}

	public static void setObject(String key, Object value) {
		if (threadLocal.get() == null) {
			Map<String, Object> map = new HashMap<String, Object>();
			threadLocal.set(map);
		}
		threadLocal.get().put(key, value);
	}

	public static Map<String, Object> getSessionMap() {
		return threadLocal.get();
	}

	@SuppressWarnings("unchecked")
	public static <T> T getObject(String key) {
		Map<String, Object> map = threadLocal.get();
		Object value = null;
		if (map != null) {
			value = map.get(key);
		}
		if (value == null) {
			value = defaultValues.get(key);
		}
		return (T)value;
	}

	public static void clearObjects() {
		threadLocal.remove();
	}
}
